package com.nari.ncps.screen.service.impl.dryport;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.dryport.DryportProdoperFluxPlan;
import com.nari.ncps.screen.mapper.dryport.DryportProdoperFluxPlanMapper;
import com.nari.ncps.screen.service.dryport.DryportProdoperFluxPlanService;
import com.nari.ncps.screen.vo.dryport.FluxPlanVO;
import com.nari.ncps.screen.vo.dryport.OwnCoalVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 引流计划表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class DryportProdoperFluxPlanServiceImpl extends ServiceImpl<DryportProdoperFluxPlanMapper, DryportProdoperFluxPlan> implements DryportProdoperFluxPlanService {

    @Autowired
    DryportProdoperFluxPlanMapper dryportProdoperFluxPlanMapper;

    @Override
    public List<FluxPlanVO> getMonthlyStatsByYearAndType(Integer year, Integer type) {
        // 从数据库查询数据
        List<DryportProdoperFluxPlan> entityList = baseMapper.getMonthlyStatsByYearAndType(year, type);
        
        // 转换为VO对象
        return entityList.stream().map(entity -> {
            FluxPlanVO vo = new FluxPlanVO();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<FluxPlanVO> getLatest12MonthsByType(Integer type) {

        List<FluxPlanVO> voList = new ArrayList<>();
            // 获取当前日期和11个月前的日期（共12个月）
            LocalDate currentDate = LocalDate.now();
            LocalDate elevenMonthsAgo = currentDate.minusMonths(11);

            // 格式化日期为 statistic_time 的格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            String startDateStr = elevenMonthsAgo.format(formatter);
            String endDateStr = currentDate.format(formatter);

            System.out.println("查询日期范围: " + startDateStr + " - " + endDateStr);

            // 构建查询条件
            LambdaQueryWrapper<DryportProdoperFluxPlan> lqw = new LambdaQueryWrapper<>();

            lqw.eq(DryportProdoperFluxPlan::getOrgCode, "100360")
                    .eq(DryportProdoperFluxPlan::getDelFlag, 0)
                    .eq(DryportProdoperFluxPlan::getDateType,1)
                    .ge(DryportProdoperFluxPlan::getStatisticTime, startDateStr)
                    .le(DryportProdoperFluxPlan::getStatisticTime, endDateStr);

            // 执行查询
            List<DryportProdoperFluxPlan> entityList = dryportProdoperFluxPlanMapper.selectList(lqw);

            // 将实体转换为VO
            if (entityList != null && !entityList.isEmpty()) {
                for(DryportProdoperFluxPlan entity : entityList) {
                    FluxPlanVO vo = new FluxPlanVO();
                    BeanUtils.copyProperties(entity, vo);
                    vo.setFluxPlanned(entity.getFluxPlanned());
                    vo.setFluxCompleted(entity.getFluxCompleted());
                    if(entity.getFluxCompleted()!=null && entity.getFluxPlanned()!=null && entity.getFluxPlanned().compareTo(BigDecimal.ZERO)>0) {
                        vo.setCompletionRate(entity.getFluxCompleted()
                                .divide(entity.getFluxPlanned(), 2, BigDecimal.ROUND_HALF_UP)
                                .setScale(2, BigDecimal.ROUND_HALF_UP));
                    } else {
                        vo.setCompletionRate(entity.getCompletionRate());
                    }
                    voList.add(vo);
                }
            }
        //处理排序
        List<FluxPlanVO> sortedList = voList.stream()
                .sorted(Comparator.comparing(FluxPlanVO::getYear)
                        .thenComparing(FluxPlanVO::getMonth))
                .collect(Collectors.toList());
        return sortedList;
    }

}
